
from crack_utilities import wordCount

# 2 helper mappings, from letters to ints and the inverse
L2I = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ",range(26)))
I2L = dict(zip(range(26),"ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

# Encrypt - rotation 
def rotate_encrypt(M,key):
	E = ""
	for c in M.upper():
		if c.isalpha(): E += I2L[ (L2I[c] + key)%26 ]
		else: E += c
	return E

def rotate_decrypt(E,key):
	M = ""
	for c in E.upper():
		if c.isalpha(): M += I2L[ (L2I[c] - key)%26 ]
		else: M += c
	return M

#######################################
# Crack rotation encrypted message
def crack_rotation(E):
    target_word_count = len(E)/4  # Tune-able parameter
    for key in range(-12,14):
        D = rotate_decrypt(E,key)
        if wordCount(D.lower()) >= target_word_count:
            print "Possible hit (key="+str(key)+"): " + D



